import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-09
 * Time:19:31
 */
class MyThreadPool{
    private BlockingQueue<Runnable> queue=new LinkedBlockingQueue<>();
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    //创建m个线程
    public MyThreadPool(int m){
        for (int i = 0; i < m; i++) {
            Thread t=new Thread(()->{
                while (true){
                    try {
                        Runnable runnable=queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }

    }
}
public class Test {
    public static void main1(String[] args) {
        ExecutorService pool= Executors.newCachedThreadPool();
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("任务");
            }
        });
    }

    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool=new MyThreadPool(10);
        for (int i = 0; i < 100; i++) {
            int task=i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(task);
                }
            });
        }
    }
}
